#coding=utf-8 
from pymongo import MongoClient 
import datetime


client = MongoClient("localhost", 27017) 
db = client["titan_db"] 
user_coll = db["user_info"] 

# 创建唯一索引 
user_coll.ensure_index("name",unique=True,dropDups=True,background=1) 
user_coll.ensure_index("id",unique=True,dropDups=True,background=1)


# 创建用户
def insert_user(_name,_passwd,_ip):
    ret = False
    try:
        id = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")
        print id
        if user_coll.insert({"id":id,"name":_name,"passwd":_passwd,"reg_ip":_ip}):
            ret = True
    except:
        pass
    return ret



# 获取用户
def get_user(_name,_passwd):
    ret = {}
    try:
        ret = user_coll.find_one({"name":_name,"passwd":_passwd})
    except:
        pass
    return ret